/*
    BibleNotebook - A bible reader software
    Copyright (C) 2014  Williams Fernando Lopez

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
 */

/*
 * BookmarkPanel.java
 *
 * Created on 10/07/2013, 05:46:59 PM
 */

package com.simplebible.ui.components;

import wl.ui.ButtonLabel;
import wl.ui.SimpleColorChooser;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent.EventType;
import wl.ui.QuickSearch;
import com.simplebible.bible.BibleReader;
import com.simplebible.bible.BibleUtils;
import wl.util.TextResourcesUtil;
import wl.util.Utils;
import com.simplebible.bible.objects.Abreviation;
import com.simplebible.bible.objects.Bible;
import com.simplebible.bible.objects.BookMark;
import com.simplebible.bible.objects.BookMarkGroup;
import com.simplebible.objects.Preferences;
import wl.util.TextPosition;
import com.simplebible.bible.objects.Verse;

/**
 *
 * @author Williams Lopez
 */
public class BookmarkPanel extends javax.swing.JPanel {

    private SimpleColorChooser colorChooser;
    Color lastSelectedColor = null;
    Abreviation lastAbreviation;
    BookMarkGroup lastGroup;
    QuickSearch<BookMarkGroup> quickSearchGroup;
    private Bible currentBible;
    private List<BookmarkPanelListener> listener = new ArrayList<BookmarkPanelListener>();

    /** Creates new form BookmarkPanel */
    public BookmarkPanel() {
        initComponents();
        colorChooser = new SimpleColorChooser();
        colorChooser.setChoseColorListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lastSelectedColor = (Color)e.getSource();
            }
        });
        panelMarcador.setVisible(false);
        
        quickSearchGroup = new QuickSearch<BookMarkGroup>(){
            @Override
            protected List<BookMarkGroup> search(String inputText) {
                try {
                    List<BookMarkGroup> list = BibleReader.getBookmarkGroup();
                    List<BookMarkGroup> newList = new ArrayList<BookMarkGroup>();
                    if(list!=null){
                        for(BookMarkGroup b : list){
                            List<TextPosition> text = Utils.searchInText(b.getGroupName(), inputText, false, false, true);
                            if(text!=null && !text.isEmpty()){
                                newList.add(b);
                            }
                        }
                    }
                    return newList;
                } catch (Exception e) {
                    return new ArrayList<BookMarkGroup>();
                }
            }
            @Override
            protected void seleccionarItem(BookMarkGroup seleccionado) {
                textGrupo.setText(seleccionado.getGroupName());
                textGrupo.commit();

            }
            @Override
            protected String getItemText(BookMarkGroup seleccionado) {
                return seleccionado.getGroupName();
            }

        };
        quickSearchGroup.setMinimunTextSizeForSearch(0);
        quickSearchGroup.setTextComponent(textGrupo.getTextComponent());

        try{
            TextResourcesUtil.registrarComponente(this);
        }catch(Exception e){
            Utils.printToLog("Error al registrar los componentes con TextResourcesUtil.registrarComponente", e, Preferences.LOG_LEVEL_ERROR);
        }

        Utils.setNextFocusableComponent(textTitulo,labelAccept);

    }

    public void filter(){
        try{
        if(lastGroup!=null){
            filtrarMarcadores();
        }else{
            filtrarGrupos();
        }
        }catch(Exception e){
            MainPanel.showNotification("mensaje_error_filtro", e);
            Utils.printToLog("Error al filtrar los grupos o marcadores", e, ALLBITS);
        }
    }

    private void filtrarMarcadores() throws IOException {
        String text = textFiltro.getText();

        if(text.trim().equals("")){
            if(lastGroup!=null){
                showBookMarks(lastGroup);
            }else{
                showBookMarkGroups();
            }
            return;
        }

        List<BookMark> found = new ArrayList<BookMark>();
        List<BookMark> list = BibleReader.getBookmarks(lastGroup);
        if (list != null) {
            for (BookMark b : list) {
                List<TextPosition> coincidencias;

                coincidencias = Utils.searchInText(b.getComentario(), text, false, false, true);
                if (coincidencias != null && !coincidencias.isEmpty()) {
                    found.add(b);
                    continue;
                }

                coincidencias = Utils.searchInText(b.getPalabrasClave(), text, false, false, true);
                if (coincidencias != null && !coincidencias.isEmpty()) {
                    found.add(b);
                    continue;
                }

                if(b.getHumanName()!=null){
                    coincidencias = Utils.searchInText(b.getHumanName(), text, false, false, true);
                    if (coincidencias != null && !coincidencias.isEmpty()) {
                        found.add(b);
                        continue;
                    }
                }
            }
        }

        String title;
        title = "\""+text+"\", "+found.size()+" "+TextResourcesUtil.getText("encontrados")+"<br/>"+TextResourcesUtil.getText("grupo") + " \""+lastGroup.getGroupName()+"\"";
        showBookMarks(title, found);

    }

    private void filtrarGrupos() throws IOException{
        String text = textFiltro.getText();

        if(text.trim().equals("")){
            showBookMarkGroups();
            return;
        }

        List<BookMarkGroup> groups = BibleReader.getBookmarkGroup();
        List<BookMarkGroup> found = new ArrayList<BookMarkGroup>();
        if(groups!=null){
            for(BookMarkGroup g : groups){
                List<TextPosition> coincidencias;

                coincidencias = Utils.searchInText(g.getGroupName(), text, false, false, true);
                if (coincidencias != null && !coincidencias.isEmpty()) {
                    found.add(g);
                    continue;
                }
            }
        }

        String title;
        //title = TextResourcesUtil.getText("filtro_grupo")+": "+text+", "+found.size()+" "+TextResourcesUtil.getText("encontrados");
        title = text+", "+found.size()+" "+TextResourcesUtil.getText("encontrados");
        showBookMarkGroups(title, found);
    }

    public void search(){

    }

    public Bible getCurrentBible() {
        return currentBible;
    }

    public void setCurrentBible(Bible currentBible) {
        this.currentBible = currentBible;
    }




    public void showNewMarcador(Bible bible,Abreviation abrv){

        Verse v = BibleUtils.findVerse(bible, abrv);


        currentBible = bible;
        panelMarcador.setVisible(true);
        textTitulo.setText("");
        textGrupo.clean();
        textGrupo.setText(TextResourcesUtil.getText("singrupo"));
        textGrupo.commit();
        textComentario.setText(v.getText());
        if(lastSelectedColor !=null ){
            labelColorResaltado.setForeground(lastSelectedColor);
        }
        lastAbreviation = abrv;
        labelVerse.setText(BibleUtils.getHumanName(currentBible, abrv));
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                textTitulo.requestFocus();
            }
        });
        
    }


    public void refresh(){
        if(lastGroup !=null){
            showBookMarks(lastGroup);
        }else{
            showBookMarkGroups();
        }

        //this.setBackground(Preferences.getInst().getBackgrounColor());
        //this.jPanel2.setBackground(Preferences.getInst().getBackgrounColor());
        this.jScrollPane2.setBackground(Preferences.getInst().getBackgrounColor());
        this.jTextPane1.setBackground(Preferences.getInst().getBackgrounColor());

    }

    @Override
    public void setVisible(boolean aFlag) {
        super.setVisible(aFlag);
        refresh();
    }



    private final void showBookMarkGroups(){
        try {
            List<BookMarkGroup> list = BibleReader.getBookmarkGroup();
            //showBookMarkGroups(TextResourcesUtil.getText("grupos_de_marcadores"),list);
            showBookMarkGroups(null,list);
        } catch (Exception e) {
            MainPanel.showNotification("error_mostrar_grupos", e);
            Utils.printToLog("Error al mostrar los grupos", e, Preferences.LOG_LEVEL_ERROR);
        }
    }

    private final void showBookMarkGroups(String title,List<BookMarkGroup> list) throws IOException{
            lastGroup = null;
            labelRegresar.setVisible(false);
            StringBuilder sb = new StringBuilder();

            String fontcolor = Utils.colorToHex(Preferences.getInst().getForegroundColor());
            String background = Utils.colorToHex(Preferences.getInst().getBackgrounColor());
            String fontsize = Preferences.getInst().getFontSize() + "";
            String fontFamily = Preferences.getInst().getFont();

            sb.append("<div style='background-color:");
            sb.append(background);
            sb.append(";font-size:");
            sb.append(fontsize);
            sb.append("px;font-family:");
            sb.append(fontFamily);
            sb.append(";color:");
            sb.append(fontcolor);
            sb.append(";'>");
            if(title!=null){
                sb.append("<h3>");
                sb.append(title);
                sb.append("</h3>");
            }

            for (BookMarkGroup b : list) {
                //sb.append("<div style='margin:2px;padding:4px;border-style:solid;border-width:1px;border-color:#C2C2CC'>");
                sb.append("<div style='margin:2px;padding:4px;'>");
                sb.append("<table>");
                sb.append("<tr>");
                sb.append("<td>");

                sb.append("<a href='group:"+b.getGroupName()+"' style='text-decoration:false;color:"+fontcolor+"'>");
                sb.append("<img border=\"0\" src=\"");
                sb.append(getClass().getResource("/simplebible/ui/img/plus.png"));
                sb.append("\" />");
                sb.append("</a>");
                sb.append("</td>");
                sb.append("<td>");
                sb.append("<a href='group:"+b.getGroupName()+"' style='text-decoration:false;color:"+fontcolor+"'>");
                sb.append(TextResourcesUtil.getText("grupo"));
                sb.append(" \"");
                sb.append(b.getGroupName());
                sb.append("\"");
                sb.append("</a>");
                sb.append("<br/><span style='font-size:8px'>Marcadores: ");
                sb.append(b.getCountMarcadores());
                sb.append("&nbsp;&nbsp;&nbsp;");
                sb.append("<a href='groupdelete:"+b.getGroupName()+"'>");
                //sb.append(TextResourcesUtil.getText("eliminar"));
                sb.append("<img border=\"0\" alt=\"Eliminar grupo\" width=\"10\" height=\"10\" src=\"");
                sb.append(getClass().getResource("/simplebible/ui/img/delete_16.png"));
                sb.append("\" />");
                sb.append("</a>");
                sb.append("</span>");


                sb.append("</td>");
                sb.append("</tr>");
                sb.append("</table>");

                sb.append("</div>");

            }
            sb.append("</div>");
            this.jTextPane1.setText(sb.toString());
    }

    private void showBookMarks(String title,List<BookMark> list){
            StringBuilder sb = new StringBuilder();

            String fontcolor = Utils.colorToHex(Preferences.getInst().getForegroundColor());
            String background = Utils.colorToHex(Preferences.getInst().getBackgrounColor());
            String fontsize = Preferences.getInst().getFontSize() + "";
            String fontFamily = Preferences.getInst().getFont();

            sb.append("<div style='background-color:");
            sb.append(background);
            sb.append(";font-size:");
            sb.append(fontsize);
            sb.append("px;font-family:");
            sb.append(fontFamily);
            sb.append(";color:");
            sb.append(fontcolor);
            sb.append(";'>");
            sb.append("<h3>");
            sb.append(title);
            sb.append("</h3>");


            for (BookMark b : list) {
                String color = "#C2C2CC";
                if(b.getColor()!=null){
                    color = Utils.colorToHex(b.getColor());
                }
                String ahref = "<a href='bookmark:"+b.getId()+"="+b.getAbreviation().toString()+"' style='color:"+fontcolor+"'>";
                sb.append("<div style='margin:2px;padding:4px;'>");
                sb.append("<table>");
                sb.append("<tr>");
                sb.append("<td>");
                sb.append(ahref);
                sb.append("<img border=\"0\" alt=\"Eliminar grupo\" src=\"");
                sb.append(getClass().getResource("/simplebible/ui/img/bookmark_red.png"));
                sb.append("\" />");
                sb.append("</a>");
                sb.append("</td>");
                sb.append("<td>");
                sb.append(ahref);
                sb.append(BibleUtils.getHumanName(this.currentBible, b.getAbreviation()));
                sb.append(" - ");
                sb.append(b.getPalabrasClave());
                sb.append("</a>");
                sb.append("<br/>");
                sb.append(b.getComentario());
                sb.append("&nbsp;&nbsp;&nbsp;");
                sb.append("<span style='font-size:8px'>");
                sb.append("<br/><a href='bookmarkdelete:"+b.getId()+"="+b.getAbreviation().toString()+"'>"+TextResourcesUtil.getText("eliminar")+"</a>");
                if(lastGroup!=null && b.getGrupos().contains(lastGroup)){
                    sb.append("&nbsp;&nbsp<a href='deletefromgroup:"+b.getId()+"="+b.getAbreviation().toString()+"="+lastGroup.getGroupName()+"'>"+TextResourcesUtil.getText("eliminar_de_grupo")+"</a>");
                }
                sb.append("</span>");

                sb.append("</td>");
                sb.append("</tr>");
                sb.append("</table>");

                sb.append("</div>");
            }
            sb.append("</div>");
            this.jTextPane1.setText(sb.toString());
    }

    
    private void showBookMarks(BookMarkGroup group) {
        try {
            lastGroup = group;
            labelRegresar.setVisible(true);
            List<BookMark> list = BibleReader.getBookmarks(group);
            if(list==null){
                list = new ArrayList<BookMark>();
            }
            String title = TextResourcesUtil.getText("grupo") + " \""+group.getGroupName()+"\"";
            showBookMarks(title, list);
        } catch (Exception e) {
            MainPanel.showNotification("error_mostrar_bookmarks", e);
            Utils.printToLog("Error al mostrar los bookmarks", e, Preferences.LOG_LEVEL_ERROR);
        }
    }


    private void addMarcador(){

        textGrupo.commit();

        if(lastAbreviation==null){
            return;
        }

        String titulo = textTitulo.getText();
        String comentario = textComentario.getText();

        BookMark bm = new BookMark();
        
        List<String> grupos = textGrupo.getInput();

        bm.setGrupos(new ArrayList<BookMarkGroup>());
        for(String s : grupos){
            if(s==null || s.trim().equals("")) continue;
            BookMarkGroup b = new BookMarkGroup();
            b.setGroupName(s);
            bm.getGrupos().add(b);
        }

        if(bm.getGrupos() == null || bm.getGrupos().isEmpty()){
            MainPanel.showNotification("debe_ingresar_un_grupo", null);
            return;
        }

        bm.setAbreviation(lastAbreviation);
        bm.setPalabrasClave(titulo);
        bm.setComentario(comentario);
        bm.setColor(lastSelectedColor);
        bm.setFecha(Calendar.getInstance());

        try {
            BibleReader.saveBookmark(bm);
        } catch (Exception e) {
            Utils.printToLog("Error al guardar el marcador:"+e.getMessage(), e, Preferences.LOG_LEVEL_ERROR);
        }
        refresh();
        panelMarcador.setVisible(false);
    }


    public void showBookmarkColorChooser(){
        if(colorChooser == null){
            colorChooser = new SimpleColorChooser();
        }

        colorChooser.setCurrentColor(Preferences.getInst().getForegroundColor());
        colorChooser.setChoseColorListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    lastSelectedColor = (Color)e.getSource();
                    if(lastSelectedColor!=null){
                        labelColorResaltado.setForeground(lastSelectedColor);
                    }
                }
            });

         javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                colorChooser.showInPopup(labelColorResaltado, 0, 0);
            }
        });
    }


    public void deleteBookMarksGroup(BookMarkGroup bg){
        final BookMarkGroup fbg = bg;
        YesNoCancelListener ync = new YesNoCancelListener() {

            public void yes(ActionEvent e) {
                try{
                    BibleReader.deleteBookmarksGroup(fbg);
                    fireBookmarkPanelListener("groupEliminated",fbg,null,true);
                    showBookMarkGroups();
                }catch(Exception ex){
                    fireBookmarkPanelListener("groupEliminated",fbg,null,false);
                    MainPanel.showNotification("error_al_borrar_grupo", ex);
                    Utils.printToLog("Error al borrar el grupo", ex, Preferences.LOG_LEVEL_ERROR);
                }
            }

            public void no(ActionEvent e) {

            }

            public void cancel(ActionEvent e) {
                
            }
        };
        MainPanel.showYesNoCancelQuestion("desea_eliminar_grupo", ync);
    }

    public void deleteBookMark(BookMark bm){
        final BookMark fmg = bm;
        YesNoCancelListener ync = new YesNoCancelListener() {

            public void yes(ActionEvent e) {
                try{
                    BibleReader.deleteBookmark(fmg);
                    fireBookmarkPanelListener("bookMarkEliminated",fmg,null,true);
                    refresh();
                }catch(Exception ex){
                    fireBookmarkPanelListener("bookMarkEliminated",fmg,null,false);
                    MainPanel.showNotification("error_al_borrar_marcador", ex);
                    Utils.printToLog("Error al borrar el marcador", ex, Preferences.LOG_LEVEL_ERROR);
                }
            }

            public void no(ActionEvent e) {

            }

            public void cancel(ActionEvent e) {

            }
        };
        MainPanel.showYesNoCancelQuestion("desea_eliminar_marcador", ync);
    }

    private void deleteBookMarkFromGroup(BookMark bm, BookMarkGroup bmg) {
        final BookMark fmg = bm;
        final BookMarkGroup fbmg = bmg;
        YesNoCancelListener ync = new YesNoCancelListener() {

            public void yes(ActionEvent e) {
                try{
                    BibleReader.deleteBookmarkFromGroup(fmg,fbmg);
                    fireBookmarkPanelListener("bookMarkEliminatedFromGroup",fmg,fbmg,true);
                    refresh();
                }catch(Exception ex){
                    fireBookmarkPanelListener("bookMarkEliminatedFromGroup",fmg,fbmg,false);
                    MainPanel.showNotification("error_al_borrar_marcador", ex);
                    Utils.printToLog("Error al borrar el marcador", ex, Preferences.LOG_LEVEL_ERROR);
                }
            }

            public void no(ActionEvent e) {

            }

            public void cancel(ActionEvent e) {

            }
        };
        MainPanel.showYesNoCancelQuestion("desea_eliminar_marcador_de_grupo", ync);
    }

    public void addBookmarkPanelListener(BookmarkPanelListener listener){
        this.listener.add(listener);
    }

    private void fireBookmarkPanelListener(String action,Object obj,Object obj2,boolean result){
        for(BookmarkPanelListener l : listener){
            if(action.equals("groupSelected")){
                l.groupSelected((BookMarkGroup)obj);
            }else if(action.equals("groupEliminated")){
                l.groupEliminated((BookMarkGroup)obj,result);
            }else if(action.equals("bookMarkSelected")){
                l.bookMarkSelected((BookMark)obj);
            }else if(action.equals("bookMarkEliminated")){
                l.bookMarkEliminated((BookMark)obj,result);
            }else if(action.equals("bookMarkEliminatedFromGroup")){
                l.bookMarkEliminatedFromGroup((BookMark)obj,(BookMarkGroup)obj2, result);
            }
        }

    }

    private void hyperlinkActivated(String url) {
        try {
            if (url.startsWith("groupdelete:")) {
                url = url.replace("groupdelete:", "");
                BookMarkGroup bmg = BibleReader.getBookMarkGroup(url);
                deleteBookMarksGroup(bmg);

            } else if (url.startsWith("group:")) {
                url = url.replace("group:", "");
                BookMarkGroup bmg = BibleReader.getBookMarkGroup(url);
                showBookMarks(bmg);
                fireBookmarkPanelListener("groupSelected",null, bmg, true);

            } else if (url.startsWith("bookmarkdelete:")) {
                url = url.replace("bookmarkdelete:", "");
                String[] datos = url.split("=");
                BookMark bm = BibleReader.getBookMark(Integer.parseInt(datos[0].trim()), Abreviation.parse(datos[1].trim()));
                deleteBookMark(bm);

            } else if (url.startsWith("bookmark:")) {
                url = url.replace("bookmark:", "");
                String[] datos = url.split("=");
                BookMark bm = BibleReader.getBookMark(Integer.parseInt(datos[0].trim()), Abreviation.parse(datos[1].trim()));
                fireBookmarkPanelListener("bookMarkSelected", bm,null, true);

            } else if(url.startsWith("deletefromgroup:")){
                url = url.replace("deletefromgroup:", "");
                String[] datos = url.split("=");
                BookMark bm = BibleReader.getBookMark(Integer.parseInt(datos[0].trim()), Abreviation.parse(datos[1].trim()));
                BookMarkGroup bmg = BibleReader.getBookMarkGroup(datos[2].trim());
                deleteBookMarkFromGroup(bm,bmg);
            }
        } catch (Exception e) {
            MainPanel.showNotification("generic_error", e);
            Utils.printToLog("Error al ejecutar la accion de hyperlink", e, Preferences.LOG_LEVEL_ERROR);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelMarcador = new javax.swing.JPanel();
        labelVerse = new javax.swing.JLabel();
        labelClose = new ButtonLabel();
        labelGroup = new javax.swing.JLabel();
        labelTitulo = new javax.swing.JLabel();
        labelComentario = new javax.swing.JLabel();
        textTitulo = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        textComentario = new javax.swing.JTextArea();
        labelColorResaltado = new javax.swing.JLabel();
        jLabel2 = new ButtonLabel();
        textGrupo = new wl.ui.TextFieldListCreator();
        labelAccept = new wl.ui.ButtonLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jPanel2 = new javax.swing.JPanel();
        labelRegresar = new ButtonLabel();
        textFiltro = new javax.swing.JTextField();
        labelFiltro = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        panelMarcador.setBackground(new java.awt.Color(255, 255, 255));
        panelMarcador.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        labelVerse.setFont(new java.awt.Font("Arial", 1, 14));
        labelVerse.setText("Génesis 1:1");

        labelClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simplebible/ui/img/delete_16.png"))); // NOI18N
        labelClose.setToolTipText("Cerrar");
        labelClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                labelCloseMouseClicked(evt);
            }
        });

        labelGroup.setText("Grupos:");
        labelGroup.setToolTipText("Escribir el nombre de los grupos separados por coma (,) ej: fé,esperanza,verdad");

        labelTitulo.setText("Título:");
        labelTitulo.setToolTipText("Palabras clave separadas por coma");

        labelComentario.setText("Comentario:");

        textTitulo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        textTitulo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textTituloKeyReleased(evt);
            }
        });

        jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));

        textComentario.setColumns(20);
        textComentario.setLineWrap(true);
        textComentario.setRows(5);
        textComentario.setBorder(null);
        textComentario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textComentarioKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textComentarioKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(textComentario);

        labelColorResaltado.setText("<html>Color de<br/>resaltado:</html>");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simplebible/ui/img/select_by_color.png"))); // NOI18N
        jLabel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel2MouseClicked(evt);
            }
        });

        textGrupo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        textGrupo.setForeground(new java.awt.Color(204, 204, 204));

        labelAccept.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simplebible/ui/img/accept.png"))); // NOI18N
        labelAccept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                labelAcceptActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelMarcadorLayout = new javax.swing.GroupLayout(panelMarcador);
        panelMarcador.setLayout(panelMarcadorLayout);
        panelMarcadorLayout.setHorizontalGroup(
            panelMarcadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMarcadorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelMarcadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelMarcadorLayout.createSequentialGroup()
                        .addComponent(labelColorResaltado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                        .addComponent(labelAccept, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelMarcadorLayout.createSequentialGroup()
                        .addGroup(panelMarcadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelComentario)
                            .addComponent(labelGroup)
                            .addComponent(labelTitulo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelMarcadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
                            .addComponent(textGrupo, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                            .addComponent(textTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelMarcadorLayout.createSequentialGroup()
                .addComponent(labelVerse, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelClose))
        );
        panelMarcadorLayout.setVerticalGroup(
            panelMarcadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelMarcadorLayout.createSequentialGroup()
                .addGroup(panelMarcadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelVerse)
                    .addComponent(labelClose))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelMarcadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelTitulo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelMarcadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelGroup)
                    .addComponent(textGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelMarcadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelComentario)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelMarcadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelColorResaltado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(labelAccept, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jScrollPane2.setBackground(new java.awt.Color(255, 255, 255));
        jScrollPane2.setBorder(null);

        jTextPane1.setBorder(null);
        jTextPane1.setContentType("text/html");
        jTextPane1.setEditable(false);
        jTextPane1.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
            public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
                hyperlinkAction(evt);
            }
        });
        jScrollPane2.setViewportView(jTextPane1);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        labelRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simplebible/ui/img/arrow_turn_left.png"))); // NOI18N
        labelRegresar.setToolTipText("Regresar a grupos");
        labelRegresar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                labelRegresarMouseClicked(evt);
            }
        });

        textFiltro.setFont(new java.awt.Font("Tahoma", 1, 14));
        textFiltro.setForeground(new java.awt.Color(102, 102, 102));
        textFiltro.setToolTipText("Filtro");
        textFiltro.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        textFiltro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textFiltroKeyReleased(evt);
            }
        });

        labelFiltro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simplebible/ui/img/filter16.png"))); // NOI18N
        labelFiltro.setToolTipText("Escriba una palabra para filtrar los datos");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelFiltro)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addComponent(labelRegresar)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(textFiltro, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(labelFiltro)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 4, Short.MAX_VALUE))
                    .addComponent(labelRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
            .addComponent(panelMarcador, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelMarcador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void labelCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelCloseMouseClicked
        panelMarcador.setVisible(false);
    }//GEN-LAST:event_labelCloseMouseClicked

    private void labelRegresarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelRegresarMouseClicked
        showBookMarkGroups();
    }//GEN-LAST:event_labelRegresarMouseClicked

    private void hyperlinkAction(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_hyperlinkAction
        if (evt.getEventType().equals(EventType.ACTIVATED)) {
            hyperlinkActivated(evt.getDescription());
        }
    }//GEN-LAST:event_hyperlinkAction

    private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2MouseClicked
        showBookmarkColorChooser();
    }//GEN-LAST:event_jLabel2MouseClicked

    private void textFiltroKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textFiltroKeyReleased
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            search();
        }else{
            filter();
        }
    }//GEN-LAST:event_textFiltroKeyReleased

    private void textTituloKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textTituloKeyReleased
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){

        }
    }//GEN-LAST:event_textTituloKeyReleased

    private void textComentarioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textComentarioKeyTyped
        
    }//GEN-LAST:event_textComentarioKeyTyped

    private void textComentarioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textComentarioKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER && (evt.getModifiers() != KeyEvent.VK_ALT || evt.getModifiers() != KeyEvent.VK_CONTROL)){
            this.labelAccept.requestFocus();
        }
        if(evt.getKeyCode() == KeyEvent.VK_TAB && (evt.getModifiers() != KeyEvent.VK_ALT || evt.getModifiers() != KeyEvent.VK_CONTROL)){
            this.labelAccept.requestFocus();
        }
    }//GEN-LAST:event_textComentarioKeyPressed

    private void labelAcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_labelAcceptActionPerformed
        addMarcador();
    }//GEN-LAST:event_labelAcceptActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane jTextPane1;
    private wl.ui.ButtonLabel labelAccept;
    private javax.swing.JLabel labelClose;
    private javax.swing.JLabel labelColorResaltado;
    private javax.swing.JLabel labelComentario;
    private javax.swing.JLabel labelFiltro;
    private javax.swing.JLabel labelGroup;
    private javax.swing.JLabel labelRegresar;
    private javax.swing.JLabel labelTitulo;
    private javax.swing.JLabel labelVerse;
    private javax.swing.JPanel panelMarcador;
    private javax.swing.JTextArea textComentario;
    private javax.swing.JTextField textFiltro;
    private wl.ui.TextFieldListCreator textGrupo;
    private javax.swing.JTextField textTitulo;
    // End of variables declaration//GEN-END:variables



    
}
